package com.example.new5.views;

import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.new5.R;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mBook ,mOlympic, mHotsearch ;
    private FrameLayout mFrame ;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        mBook.setOnClickListener(this);
        mOlympic.setOnClickListener(this);
        mHotsearch.setOnClickListener(this);
    }
    private void init(){
        mBook = findViewById(R.id.book);
        mOlympic = findViewById(R.id.olympic) ;
        mHotsearch = findViewById(R.id.hotsearch) ;
        mFrame = findViewById(R.id.frame);
        mBook.setSelected(true);
        replaceFragment(new BookFragment());
    }
    @Override
    public void onClick(View v) {
        // 先取消所有选项的选中状态
        mBook.setSelected(false);
        mOlympic.setSelected(false);
        mHotsearch.setSelected(false);

        // 根据点击的 ID 设置对应选项为选中状态
        if (v.getId() == R.id.book){
            mBook.setSelected(true);
            replaceFragment(new BookFragment());
        } else if (v.getId() == R.id.olympic) {
            mOlympic.setSelected(true);
            replaceFragment(new OlympicFragment());
        } else if (v.getId() == R.id.hotsearch) {
            mHotsearch.setSelected(true);
            replaceFragment(new HotsearchFragment());
        }
    }
    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frame , fragment);
        transaction.commit() ;
    }
}

